package com.fy.dao.impl;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import com.fy.dao.DaoException;
import com.fy.dao.NewsDao;
import com.fy.model.News;
import com.fy.util.DbUtil;

/**
 * 新闻DAO实现类
 * @author 小叶
 */
public class NewsDaoImpl implements NewsDao {
	private Connection conn = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;
	@Override
	public void insert(News news) {
		String sql = "insert into news(newsid,title,author,source,content) values (news_seq.nextval,?,?,?,?)";
		try {
			conn = DbUtil.getMyConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, news.getTitle());
			ps.setString(2, news.getAuthor());
			ps.setString(3, news.getSource());
			ps.setString(4, news.getContent());
			ps.execute();
		} catch (SQLException e) {
			throw new DaoException(e.getMessage(), e);
		} finally {
			DbUtil.close(conn, ps, null);
		}
	}
	@Override
	public void delete(Integer newsid) {
		String sql = "delete from news where newsid=?";
		try {
			conn = DbUtil.getMyConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, newsid);
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new DaoException(e.getMessage(), e);
		} finally {
			DbUtil.close(conn, ps, null);
		}
	}
	@Override
	public void update(News news) {
		String sql = "update news set title=?,author=?,source=?,content=? where newsid=?";
		try {
			conn = DbUtil.getMyConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, news.getTitle());
			ps.setString(2, news.getAuthor());
			ps.setString(3, news.getSource());
			ps.setString(4, news.getContent());
			ps.setInt(5, news.getNewsid());
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new DaoException(e.getMessage(), e);
		} finally {
			DbUtil.close(conn, ps, null);
		}
	}
	@Override
	public List<News> find() {
		String sql = "select * from news";
		List<News> list = new ArrayList<News>();
		try {
			conn = DbUtil.getMyConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				News news = new News();
				news.setNewsid(rs.getInt(1));
				news.setTitle(rs.getString(2));
				news.setAuthor(rs.getString(3));
				news.setSource(rs.getString(4));
				news.setContent(rs.getString(5));
				news.setTime(rs.getDate(6));
				list.add(news);
			}
		} catch (SQLException e) {
			throw new DaoException(e.getMessage(), e);
		} finally {
			DbUtil.close(conn, ps, rs);
		}
		return list;
	}
	@Override
	public News findById(Integer id) {
		String sql = "select * from news where newsid=" + id;
		News news = new News();
		try {
			conn = DbUtil.getMyConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				news.setNewsid(rs.getInt(1));
				news.setTitle(rs.getString(2));
				news.setAuthor(rs.getString(3));
				news.setSource(rs.getString(4));
				news.setContent(rs.getString(5));
				news.setTime(rs.getDate(6));
			}
		} catch (SQLException e) {
			throw new DaoException(e.getMessage(), e);
		} finally {
			DbUtil.close(conn, ps, rs);
		}
		return news;
	}
}